package com.example.lib_visitor

import android.content.Intent
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.CompoundButton
import android.widget.LinearLayout
import android.widget.PopupWindow
import androidx.activity.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.viewbinding.ViewBinding
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.lib_common.mvi.BaseActivity
import com.example.lib_visitor.databinding.ActivityVisitorBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/visitor/VisitorActivity")
class VisitorActivity : BaseActivity<ActivityVisitorBinding,VisitorViewModel>(){
    var parkId =0
    override fun createVM(): VisitorViewModel {
        return VisitorViewModel()
    }

    override fun createViewBinding(): ViewBinding {
        return ActivityVisitorBinding.inflate(inflater)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        bindState()
        v.visitorName.text=SPUtils.getInstance().getString("uName")
        v.visitorDepartment.text=SPUtils.getInstance().getString("department")
        v.visitorShare.setOnClickListener { share() }
        v.isCar.setOnCheckedChangeListener(object : CompoundButton.OnCheckedChangeListener{
            override fun onCheckedChanged(p0: CompoundButton?, p1: Boolean) {
            if (p1){
                v.visitorParking.visibility=View.VISIBLE
                v.carNum.visibility=View.VISIBLE
            }else{
                v.visitorParking.visibility=View.GONE
                v.carNum.visibility=View.GONE
            }
            }
        })
        v.visitorSpace.setOnClickListener {
            ARouter.getInstance().build("/car/ActivityApplyParking")
                .withString("parkNum",v.visitorCarNum.text.toString())
                .navigation(this@VisitorActivity,123)
        }
    }

    private fun bindState() {
        lifecycleScope.launch(Dispatchers.Main){
            repeatOnLifecycle(Lifecycle.State.STARTED){
                vm.mutableContainer.uiStateFlow.collect{
                    if (it.entity.statuesCode==200){

                    }
                }
            }
        }
    }

    private fun addVisitor() {
        var map = HashMap<String,Any>()
        map["carNum"]=v.visitorCarNum.text.toString()
        map["parkId"]=parkId
        map["userId"]=SPUtils.getInstance().getInt("uId")
        map["visitorDescribe"]=v.visitorTodo.text.toString()
        map["visitorEnd"]=""+System.currentTimeMillis()/1000
        map["visitorId"]=0
        map["visitorStart"]=""+System.currentTimeMillis()/1000
        map["visitorTime"]=""+System.currentTimeMillis()/1000
        vm.addVisitor(map)
    }

    private fun share() {
        val inflate = LayoutInflater.from(this).inflate(R.layout.item_share, null, false)
        val popupWindow =PopupWindow()
        popupWindow.isFocusable=true
        popupWindow.width=LinearLayout.LayoutParams.MATCH_PARENT
        popupWindow.height=800
        popupWindow.contentView=inflate
        popupWindow.showAtLocation(v.visitorShare,Gravity.BOTTOM,0,0)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode==123&&resultCode==123){
            if (data!!.getStringExtra("statues").toString()=="成功"){
                parkId = data.getIntExtra("parkId", 1)
                v.subVisitor.setOnClickListener { addVisitor() }
            }else{
                v.subVisitor.setOnClickListener { ToastUtils.showShort("车位申请失败") }
            }
        }
    }
}